﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.Entity;
using Domain.Concrete;
using Domain.Abstract;

namespace Pro260Website.Controllers
{ 
    public class NewsController : Controller
    {
        private EFDbContext db = EFDbContext.Instance;
        private IGameRepository Repo { get; set; }
        private ICompanyRepository _companyRepository;

        private const int ELEMENTS_PER_PAGE = 10;

        public NewsController(IGameRepository repository, ICompanyRepository companyRepository)
        {
            EFDbContext mainContext = EFDbContext.Instance;
            Repo = repository;
            Repo.Context = mainContext;
            _companyRepository = companyRepository;
            _companyRepository.Context = mainContext;
        }
        //
        // GET: /News/

        public ViewResult ViewGameNews(int gameId)
        {
            return View("ViewNews", Repo.FindById(gameId).Articles.ToList());
        }

        public ViewResult ViewPlatformNews(int platformId)
        {
            return View("ViewNews", db.Platforms.Include("Articles").FirstOrDefault(p => p.PlatformID == platformId).Articles.ToList());
        }

        public ViewResult ViewCompanyNews(int companyId)
        {
            return View("ViewNews", _companyRepository.FindCompany(companyId));
        }

        public ViewResult CurrentNewsWidget()
        {
            return View(db.NewsArticles.OrderBy(x => x.PostedDate).Take(ELEMENTS_PER_PAGE));
        }

        public ViewResult Listings()
        {
            return View("ViewNews", db.NewsArticles);
        }

        public ViewResult CompanyListings()
        {
            var articles = new List<CompanyNewsArticle>();

            var list = db.Companies.Include("Articles").Where(c => c.Articles.Count > 0).ToList();

            foreach(Company c in list)
            {
                articles.AddRange(c.Articles);
            }

            return View("ViewNews", articles);
        }

        //
        // GET: /News/Details/5

        public ViewResult Details(int id)
        {
            NewsArticle newsarticle = db.NewsArticles.Find(id);
            return View(newsarticle);
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}